#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    m_Manager = new Manager(this);
    m_Rendering = false;
    m_Renderer = new Renderer(0, this);
    m_Renderer->show();

    m_Future = new QFuture<void>;
    m_Watcher = new QFutureWatcher<void>;

    connect(m_Watcher, SIGNAL(finished()),
            this, SLOT(FrameRendered()));

    dwFrames = 0;
    dwCurrentTime = 0;
    dwLastUpdateTime = 0;
    dwElapsedTime = 0;
    szFPS[0] = '\0';
    m_Iterator = 0;

    setGeometry(QRect(100, 300, 551, 309));
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_pushButton_clicked()
{
    m_Rendering = !m_Rendering;

    if (m_Rendering)
    {
        ui->pushButton->setText("Stop");

        *m_Future = QtConcurrent::run(renderFrame);
        m_Watcher->setFuture(*m_Future);
    }
    else
    {
        ui->pushButton->setText("Render");
    }
}
/*
QPixmap MainWindow::Uint2Pixmap(uint** _data)
{
    int width = m_Demo->m_Rasterizer->GetBuffor()->GetWidth();
    int height = m_Demo->m_Rasterizer->GetBuffor()->GetHeight();
    QPixmap *retPixmap = new QPixmap(width, height);
    QImage *img = new QImage(retPixmap->toImage());

    for (int i = 0; i < width; i++)
        for (int j = 0; j < height; j++)
        {
            img->setPixel(i, j, _data[width - 1 - i][height - 1 - j]);
        }

    retPixmap = new QPixmap(QPixmap::fromImage(*img));

    return *retPixmap;
}

void MainWindow::RenderFrame()
{
    uint** data = m_Demo->Render();
    QPixmap actualScene = Uint2Pixmap(data);
    emit Render(actualScene);
}
*/
void MainWindow::FrameRendered()
{
    if (m_Rendering)
    {
        *m_Future = QtConcurrent::run(renderFrame);
        m_Watcher->setFuture(*m_Future);
        CountFPS();
        UpdateInfos();
    }
}

void MainWindow::CountFPS()
{
    // Calculate the number of frames per one second:

    dwFrames++;
    dwCurrentTime = GetTickCount(); // Even better to use timeGetTime()
    dwElapsedTime = dwCurrentTime - dwLastUpdateTime;
    QString fps = QString();

    if(dwElapsedTime >= 1000)
    {
        //wsprintf(szFPS, _T("FPS = %u"), (UINT)(dwFrames * 1000.0 / dwElapsedTime));
        uint fpsN = (UINT)(dwFrames * 1000.0 / dwElapsedTime);
        qDebug() << fpsN;
        fps = QString::number(fpsN);
        dwFrames = 0;
        dwLastUpdateTime = dwCurrentTime;
        ui->label_2->setText(fps);
    }
}

void MainWindow::UpdateInfos()
{
    float  lightPosX = m_Manager->m_Demo->m_Rasterizer->m_Vp->m_PointLight.GetPosition()->x;
    float  lightPosY = m_Manager->m_Demo->m_Rasterizer->m_Vp->m_PointLight.GetPosition()->y;
    float  lightPosZ = m_Manager->m_Demo->m_Rasterizer->m_Vp->m_PointLight.GetPosition()->z;

    QString lightPosStr = QString::fromStdString("(") + QString::number(lightPosX) + QString::fromStdString("; ") + QString::number(lightPosY) + QString::fromStdString("; ") + QString::number(lightPosZ) + QString::fromStdString(")");
    ui->label_3->setText(lightPosStr);
}

void MainWindow::on_pushButton_2_clicked()
{
    m_Manager->m_ActualScene = 0;
}

void MainWindow::on_pushButton_3_clicked()
{
    m_Manager->m_ActualScene = 1;
}

void MainWindow::on_pushButton_4_clicked()
{
    m_Manager->m_ActualScene = 2;
}
